import { Controller } from 'egg';

class PaintController extends Controller {
    /**
     * 查询画像设置列表
     */
    public async searchPaintList() {
        const { ctx } = this;
        const paintList = await this.service.paintService.searchPaintList();
        ctx.body = { code: 1, msg: '查询成功', data: paintList };
    }

    /**
     * 创建画像
     */
    public async createPaint() {
        const { ctx } = this;
        const param = ctx.request.body;
        let paint: any = {};
        let paintValueList: any = [];
        for (const i in param) {
            if (!param[i] || i === 'paintValueList') {
                continue;
            }
            paint[i] = param[i];
        }
        if (!param.paintValueList) {
            paintValueList = [];
        } else {
            try {
                paintValueList = JSON.parse(param.paintValueList);
            } catch (e) {
                ctx.body = this.ctx.helper.responseErrorParam('画像值格式不正确');
                return;
            }
        }
        await this.ctx.service.paintService.createPaint(paint, paintValueList);
        ctx.body = { code: 1, msg: '添加成功' };
    }

    /**
     * 启用、停用画像
     */
    public async startAndStopPaint() {
        const { ctx } = this;
        const param = ctx.request.body;
        let type = param.type;
        const paint_id = param.paint_id;
        if (!type || !paint_id) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        type = Number(param.type);
        if (type !== 1 && type !== 2) {
            ctx.body = { code: 4, msg: '无法识别的操作类型' };
            return;
        }
        await this.ctx.service.paintService.startAndStopPaint(type, paint_id);
        let text: string = '';
        if (type === 1) {
            text = '已启用';
        } else if (type === 2) {
            text = '已停用';
        }
        ctx.body = { code: 1, msg: text };

    }

    /**
     * 上移、下移画像
     */
    public async removePaint() {
        const { ctx } = this;
        const param = ctx.request.body;
        const paint_id = param.paint_id;
        let paint_order = param.paint_order;
        let type = param.type;
        if (!paint_id || !paint_order || !type) {//判断参数是否为空
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        paint_order = Number(param.paint_order);
        type = Number(param.type);
        if (type !== 1 && type !== 2) {//判断操作类型
            ctx.body = { code: 4, msg: '无法识别的操作类型' };
            return;
        }
        if (typeof (paint_order) !== "number" || isNaN(paint_order)) {
            ctx.body = { code: 4, msg: '序号请输入数字' };
            return;
        }
        await this.ctx.service.paintService.removePaint(paint_id, paint_order, type);
        ctx.body = { code: 1, msg: '成功移动' };
    }

    /**
     * 根据id查找画像
     */
    public async searchPaintById() {
        const { ctx } = this;
        const param = ctx.request.body;
        const paint_id = param.paint_id;
        if (!paint_id) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        const result = await this.ctx.service.paintService.searchPaintById(paint_id);
        ctx.body = { code: 1, msg: '查询成功', data: result };
    }

    /**
     * 根据id查找画像值列表
     */
    public async searchPaintValueById() {
        const { ctx } = this;
        const param = ctx.request.body;
        const paint_id = param.paint_id;
        if (!paint_id) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        const result = await this.ctx.service.paintService.searchPaintValueById(paint_id);
        ctx.body = { code: 1, msg: '查询成功', data: result };
    }

    /**
     * 修改画像设置
     */
    public async updatePaint() {
        const { ctx } = this;
        const param = ctx.request.body;
        const paint_id = param.paint_id;
        const paint_title = param.paint_title;
        const paint_question = param.paint_question;
        const paint_type = param.paint_type;
        let paint_selected_count = param.paint_selected_count;
        if (!paint_id || !paint_title || !paint_question || !paint_type || !paint_selected_count) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        paint_selected_count = Number(param.paint_selected_count);
        if (typeof (paint_selected_count) !== "number" || isNaN(paint_selected_count)) {
            ctx.body = { code: 4, msg: '可选数请输入数字' };
            return;
        }
        await this.ctx.service.paintService.updatePaint(param);
        ctx.body = { code: 1, msg: '更新成功' };
    }

    /**
     * 添加画像值
     */
    public async createPaintValue() {
        const { ctx } = this;
        const param = ctx.request.body;
        const paint_id = param.paint_id;
        const paint_value_title = param.paint_value_title;
        if (!paint_value_title || !paint_id) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        await this.ctx.service.paintService.createPaintValue(param);
        ctx.body = { code: 1, msg: '添加成功' };
    }

    /**
     * 删除画像值
     */
    public async deletePaintValue() {
        const { ctx } = this;
        const param = ctx.request.body;
        const paint_id = param.paint_id;
        const paint_value_id = param.paint_value_id;
        let paint_value_order = param.paint_value_order;
        if (!paint_value_order || !paint_id || !paint_value_id) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        paint_value_order = Number(param.paint_value_order);
        if (typeof (paint_value_order) !== "number") {
            ctx.body = { code: 4, msg: '序号请输入数字' };
            return;
        }
        await this.ctx.service.paintService.deletePaintValue(param);
        ctx.body = { code: 1, msg: '删除成功' };
    }

    /**
     * 上移、下移画像值
     */
    public async removePaintValue() {
        const { ctx } = this;
        const param = ctx.request.body;
        const paint_id = param.paint_id;
        const paint_value_id = param.paint_value_id;
        let paint_value_order = param.paint_value_order;
        let type = param.type;
        if (!paint_id || !paint_value_id || !paint_value_order || !type) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        type = Number(param.type);
        paint_value_order = Number(param.paint_value_order);
        if (type !== 1 && type !== 2) {
            ctx.body = { code: 4, msg: '无法识别的操作类型' };
            return;
        }
        if (typeof (paint_value_order) !== "number" || isNaN(paint_value_order)) {
            ctx.body = { code: 4, msg: '序号请输入数字' };
            return;
        }
        await this.ctx.service.paintService.removePaintValue(paint_id, paint_value_id, paint_value_order, type);
        ctx.body = { code: 1, msg: '移动成功' };
    }

    /**
     * 查找画像及画像值
     */
    public async searchPaintAndValue() {
        const { ctx } = this;
        const result = await this.ctx.service.paintService.searchPaintAndValue();
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }

    /**
     * 根据用户id查找画像值
     */
    public async searchPaintValueByUserId() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        let result = await ctx.service.paintService.searchPaintValueByUserId(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 修改用户画像值
     */
    public async updateUserPaintValue() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
                paint_id: 'string',
                paint_type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.paintService.updateUserPaintValue(request.body);
        ctx.body = ctx.helper.responseSuccess('更新成功');
    }
}
export default PaintController;